/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI;

import BL.Usuario;
import java.awt.event.KeyEvent;
import java.awt.image.ImageObserver;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author carlos saborio
 */
public class Login extends javax.swing.JFrame {

    /**
     * Creates new form Login
     */
    public Login() {
        initComponents();
        this.setLocationRelativeTo(null);
        this.lblusuario.setVisible(false);
        this.lblpwd.setVisible(false);
        this.lblLog.setVisible(false);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        btnIngresar = new org.edisoncor.gui.button.ButtonRound();
        txtusuario = new org.edisoncor.gui.textField.TextFieldRound();
        txtPassword = new org.edisoncor.gui.passwordField.PasswordFieldRound();
        lblusuario = new javax.swing.JLabel();
        lblpwd = new javax.swing.JLabel();
        lblLog = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Inicio Sesión");
        setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel1.setText("Contraseña:");

        jLabel2.setFont(new java.awt.Font("Cambria", 3, 14)); // NOI18N
        jLabel2.setText("Usuario:");

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/1403769923_lock.png"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(108, 131, 177));
        jPanel2.setForeground(new java.awt.Color(0, 102, 153));

        jLabel6.setFont(new java.awt.Font("Cambria", 3, 24)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Inicio de Sesión");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(278, 278, 278))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel6)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        btnIngresar.setBackground(new java.awt.Color(204, 204, 255));
        btnIngresar.setText("Ingresar");
        btnIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarActionPerformed(evt);
            }
        });

        txtusuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtusuarioKeyPressed(evt);
            }
        });

        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });

        lblusuario.setForeground(new java.awt.Color(204, 0, 0));
        lblusuario.setText("*");

        lblpwd.setForeground(new java.awt.Color(204, 0, 0));
        lblpwd.setText("*");

        lblLog.setForeground(new java.awt.Color(204, 0, 0));
        lblLog.setText("Usuario o contraseña incorrecta. Favor ingresarlo de nuevo.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtusuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblusuario)
                                    .addComponent(lblpwd)))
                            .addComponent(lblLog))
                        .addGap(0, 132, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 63, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtusuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblusuario))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblpwd))
                        .addGap(18, 18, 18)
                        .addComponent(lblLog)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarActionPerformed
        Login();
    }//GEN-LAST:event_btnIngresarActionPerformed

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        int key=evt.getKeyCode();
        if(key==KeyEvent.VK_ENTER){
            Login();
        }
    }//GEN-LAST:event_txtPasswordKeyPressed

    private void txtusuarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtusuarioKeyPressed
        int key=evt.getKeyCode();
        if(key==KeyEvent.VK_ENTER){
            Login();
        }
         
    }//GEN-LAST:event_txtusuarioKeyPressed
    
   private void Login(){
       int evaluacion=EvaluarCampos();
       switch(evaluacion){
           case -1:
             validacion();
             break;
           case 0:
               this.lblusuario.setVisible(true);
               break;
           case 1:
               this.lblpwd.setVisible(true);
               break;
           case 2:
               this.lblpwd.setVisible(true);
               this.lblusuario.setVisible(true);
               
       }
       
   } 
   
   private void validacion(){
       this.lblLog.setVisible(false);
        String usu = this.txtusuario.getText();
       String pwd =String.valueOf(this.txtPassword.getPassword());
       
        try {
            Usuario t = Usuario.traerUsuario(usu, pwd);
            if(t==null){
                this.lblLog.setVisible(true);
            }else{
                MenuPrincipla men = new MenuPrincipla(t);
                men.setVisible(true);
                this.dispose();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null,"Error: "+ex.getMessage(), "Error en la base de datos", JOptionPane.ERROR_MESSAGE);
        }catch(ParseException ex){
            JOptionPane.showMessageDialog(null,"Error: "+ex.getMessage(), "Error en la base de datos", JOptionPane.ERROR_MESSAGE);
        }
      
   }
   private int EvaluarCampos(){
       this.lblusuario.setVisible(false);
       this.lblpwd.setVisible(false);
       String usu = this.txtusuario.getText();
       String pwd =String.valueOf(this.txtPassword.getPassword());
       int valor = -1; //cuando es -1 ambos campos estan llenos
       if(usu.isEmpty()&&!pwd.isEmpty()){//cuando el usuario esta vacio y el password completado retorna 0
           valor=0;
       }else{
           if(!usu.isEmpty()&&pwd.isEmpty()){//cuando el usuario esta lleno y el pwd vacion retorna 1
               valor = 1;
           }else{ //pero cuando ambas estan vacias retorna 2
             if(usu.isEmpty()&&pwd.isEmpty()){
               valor =2;
              }
           }
       }
       return valor;
   }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.edisoncor.gui.button.ButtonRound btnIngresar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblLog;
    private javax.swing.JLabel lblpwd;
    private javax.swing.JLabel lblusuario;
    private org.edisoncor.gui.passwordField.PasswordFieldRound txtPassword;
    private org.edisoncor.gui.textField.TextFieldRound txtusuario;
    // End of variables declaration//GEN-END:variables
}
